package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.ContractImageBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.ContractImageMapper;
import com.molichuxing.services.infrastructure.dto.request.create.ContractImageCreateDto;
import com.molichuxing.services.infrastructure.dto.response.ContractImageDto;
import com.molichuxing.services.infrastructure.service.ContractImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 合同附件表 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-09-11
 */
@Service("contractImageService")
public class ContractImageServiceImpl implements ContractImageService {

    @Autowired
    ContractImageMapper contractImageMapper;


    /**
     * 合同附件添加
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public Boolean createContractImageBatch(List<ContractImageCreateDto> list) throws Exception {
        if (null == list || list.size() < 1) {
            return false;
        }

        int result = contractImageMapper.createBatch(list);
        if (result == list.size()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取合同附件list
     *
     * @param contractId
     * @return
     */
    @Override
    public List<ContractImageDto> getContractImageList(Integer contractId) {
        if (null == contractId) {
            return null;
        }

        List<ContractImageBean> list = contractImageMapper.getListByContractId(contractId);
        if (null == list || list.size() < 1) {
            return null;
        }
        return Convert.toContractImageDtoList(list);
    }

    /**
     * 附件名称
     *
     * @param name
     * @return
     */
    @Override
    public Boolean getImageNameExist(String name) {
        if (StringUtils.isBlank(name)) {
            throw new BizException("附件名称为空");
        }

        List<ContractImageBean> list = contractImageMapper.getListByName(name);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }


}
